using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using ENTITIES;

namespace DAL
{
 public   class LoaiMHDAL
    {
     
        private SqlConnection cn = null;
        public LoaiMHDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
       public void Insert (TBLoaiMH mh)
        {
            SqlCommand cm = new SqlCommand("LoaiMH_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Ten",mh.ten);
            
           
            cm.ExecuteNonQuery();
        }
        public void Update(TBLoaiMH mh)
        {
            SqlCommand cm = new SqlCommand("LoaiMH_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaLoaiMH", mh.maloaimh);
            cm.Parameters.AddWithValue("@Ten", mh.ten);
          
          
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("LoaiMH_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaLoaiMH", iD);
            cm.ExecuteNonQuery();
        }

        public tbLoaiMHCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("LoaiMH_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbLoaiMHCollection lmhCollection = new tbLoaiMHCollection();

            while (dr.Read())
            {
                TBLoaiMH loaimathang = new TBLoaiMH();
                loaimathang.maloaimh = int.Parse(dr.GetValue(0).ToString());
                loaimathang.ten = dr.GetValue(1).ToString();
             
                lmhCollection.Add(loaimathang);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return lmhCollection;
        }
    }
    }

